package com.jing.system.comm.contants;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 常用的类
 * @author jing.yue
 * @version 1.0
 * @since 2012-08-29
 */
public class SystemCons {

	private static Logger log = Logger.getLogger(SystemCons.class);

	private final static String CONTANTS_PROPERTIES_URL = "/contants/contants.properties";
	//private final static String CONTANTS_PROPERTIES_URL = "/contants/contants_tools.properties";

	/** 编码: UTF-8 */
	public final static String ENCODE_UTF_8 = "UTF-8";

	/** 第二次调用微博接口等待时间[单位: 毫秒] */
	public static final int THREAD_SLEEP_DATE = 5000;

	/** 第二次调用微博接口查询数据等待时间[单位: 毫秒] */
	public static final int THREAD_QUERY_SLEEP_DATE = 3500;

	/** 获取参与互粉的记录[默认为100] */
	public static final int JOINMP_USERINFO_NUM = 100;

	/** 分享微博的内容 */
	public final static String getUserShareWeiboContent() {
		String content = WeiboCons.user_share_weibo_content.replaceAll("weibo_tools_index", WeiboCons.weibo_tools_index);
		return content;
	}

	/**
	 * 参与互粉发送的微博内容
	 * @param addFansNum
	 * @return
	 */
	public static String getWeiboMpContent(Integer addFansNum) {
		String content = WeiboCons.weibo_mp_content.replaceAll("addFansNum", addFansNum.toString());
		content = content.replaceAll("weibo_tools_index", WeiboCons.weibo_tools_index);
		return content;
	}

	private static Properties properties;
	static {
		//属性集合对象
		properties = new Properties();
		//将属性文件流装载到Properties对象中
		try {
			properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(CONTANTS_PROPERTIES_URL));
		} catch (FileNotFoundException e) {
			log.error("读取属性文件: 失败\t原因: 文件路径错误或者文件不存在");
		} catch (IOException e) {
			log.error("装载文件: 失败\t原因: " + e.getMessage());
		}
	}

	/**
	 * 获取公共的属性文件的内容
	 * @param name			key的名字
	 * @param defaultValue	默认的值
	 * @return
	 */
	public static String getContantValue(String name, String defaultValue) {
		// TODO Auto-generated method stub
		try {
			String value = properties.getProperty(name, defaultValue);
			log.info("读取属性文件和值: " + name + " = " + value);
			return value;
		} catch (Exception e) {
			log.error("未知异常\t原因: " + e.getMessage());
			e.printStackTrace();
		}
		return defaultValue;
	}
}